<?php
namespace app\admin\model;
use app\common\model\AdminBase;
class Log extends AdminBase{
    protected $log = [];//日志信息
    protected $log_type = [
        'admin_system'    => '系统',//系统模块的日志
        'admin_user'      => '后台用户',//后台用户模块的日志
        'admin_config'    => '系统配置',
        'admin_auth'      =>'权限管理',
        'admin_stream'    => '直播'
    ];
    protected $tableName = 'system_log';
    protected $fields = [
        0   =>  'log_id', 
        1   =>  'uid', 
        2   =>  'content', 
        3   =>  'type',
        4   =>  'key',
        5   =>  'ctime'
    ];
    public function __construct(string $content,string $type,array $ext){
        parent::__construct();
        $ext && $this->setLogs($ext);
        $content && $logs['content'] = $content;
        $type && $logs['type'] = $type;
        $logs['ctime'] = NOW_TIME;
        $logs['uid'] = (int)decrypt_id(session('aid'));
        $this->setLogs($logs);
        return $this;
    }
    /**
     * @name 添加日志记录
     * @param string $content 日志详细内容
     * @param string $type 日志类型
     * @param array $ext 日志扩展信息--除日志内容和类型之外的信息
     * @return boolean 添加是否成功
     */
    public function addLog(string $content,string $type,array $ext){
        if(!decrypt_id(session('aid'))) return false;
        $ext && $this->setLogs($ext);
        $logs = [];
        $content && $logs['content'] = $content;
        $type && $logs['type'] = $type;
        $this->setLogs($logs);
        $this->log = $this->validateData($this->log);
        if(!$this->log['type'] || !array_key_exists($this->log['type'],$this->log_type) ){
            $this->error = '日志类别不能为空';
            return false;
        }
        if($this->add($this->log)){
            return true;
        }
        return false;
    }
    /**
     * @name 设置log----设置保存的日志信息
     * @param string $filed 日志键名
     * @param string $value 日志键值
     * @return array 当前已经设置的日志信息
     */
    protected function setLog($filed = '',$value = ''){
        $this->log[strtolower($filed)] = $value;
        return $this->log;
    }
    /**
     * @name 设置log---通过数组设置
     * @param array $log 日志数据数组
     */
    protected function setLogs(array $logs){
        if(!empty($logs)){
            foreach($logs as $k=>$v){
                $this->setLog($k,$v);
            }
        }
    }
    /**
     * @name 添加日志类别
     * @param string $type 日志类型字段
     * @param string $desc 日志类型中文描述
     * @return array 当前已有的日志类型
     */
    public function addLogType(string $type,string $desc){
        $this->log_type[$type] = $desc;
        return $this->log_type;
    }
    /**
     * @name 获取日志信息
     * @param boolean $getAll 是否获取全部
     * @return array 日志信息数据数组
     */
    public function getLogList($getAll = false){
        if($getAll === true){
            $list['data'] = $this->order('ctime desc')->select();
        }else{
            $list = $this->order('ctime desc')->findPage();
        }
        if($list['data']){
            $adminUser = model('admin/AdminUser');
            foreach($list['data'] as &$v){
                $v['uname'] = $adminUser->getUserNameByUid($v['uid']);
                $v['to_uid'] && $v['to_uname'] = $adminUser->getUserNameByUid($v['to_uid']);
                $v['ctime'] = date('Y-m-d H:i:s',$v['ctime']);
                $v['type'] = $this->log_type[$v['type']];
            }
        }
        return $list;
    }
}